﻿using AuthCenter_HuangJingtao.Infrastructure.Imterfaces;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthCenter_HuangJingtao.Infrastructure.Implements
{
    public class BaseReponsitory<T> : IBaseReponsitory<T> where T : class
    {
        DbdataContext context;
        DbSet<T> Dbset;

        public BaseReponsitory(DbdataContext context)
        {
            this.context = context;
            Dbset = context.Set<T>();
        }

        /// <summary>
        /// 添加方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> AddAsync(T entity)
        {
           await context.Set<T>().AddAsync(entity);
           return await context.SaveChangesAsync();
        }

        /// <summary>
        /// 添加修改方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> UpdateAsync(T entity)
        {
            context.Set<T>().Update(entity);    
       
            return await context.SaveChangesAsync();
        }

        /// <summary>
        ///显示方法
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> GetQuery() 
        {
            return context.Set<T>().AsQueryable();
        }

        /// <summary>
        /// 删除方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(T entity)
        {
            context.Set<T>().Remove(entity);
            return await context.SaveChangesAsync();
        }

        /// <summary>
        /// 循环添加方法
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public async Task<int> AddRangeAsync(List<T> entities)
        {
            await context.Set<T>().AddRangeAsync(entities);
            return await context.SaveChangesAsync();
        }

        /// <summary>
        /// 循环删除方法
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public async Task<int> DeleteRangeAsync(List<T> entities)
        {
            context.Set<T>().RemoveRange(entities);
            return await context.SaveChangesAsync();
        }
    }
}
